package com.avic.rong.wedgit.customView;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.avic.rong.R;
import com.avic.rong.utils.DensityUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Creater: Created by wangyz on 2019/9/7.
 * Email: 936804097@qq.com
 */
public class TypeGroupView extends LinearLayout implements View.OnClickListener {


    private Context context;
    private LinearLayout ll_top;
    private TextView tv_type_standard;
    private TextView tv_type_book;
    private TextView tv_type_document;
    private TextView tv_type_software;
    private List<TextView> textViewList = new ArrayList<>();

    public TypeGroupView(Context context) {
        this(context, null);
    }

    public TypeGroupView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public TypeGroupView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
        View myView = LayoutInflater.from(context).inflate(R.layout.view_type_group, null);


        tv_type_standard = myView.findViewById(R.id.tv_type_standard);
        tv_type_book = myView.findViewById(R.id.tv_type_book);
        tv_type_document = myView.findViewById(R.id.tv_type_document);
        tv_type_software = myView.findViewById(R.id.tv_type_software);
        ll_top = myView.findViewById(R.id.ll_top);


        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(DensityUtils.getWindowWidth(context), DensityUtils.dip2px(context, 56));
        ll_top.setOrientation(LinearLayout.HORIZONTAL);
        ll_top.setLayoutParams(layoutParams);

        LinearLayout.LayoutParams textLayoutParams = (LinearLayout.LayoutParams) tv_type_standard.getLayoutParams();
        textLayoutParams.weight = 1;
        textLayoutParams.leftMargin = DensityUtils.dp2px(context, 7);
        textLayoutParams.rightMargin = DensityUtils.dp2px(context, 7);
        tv_type_standard.setLayoutParams(textLayoutParams);
        tv_type_book.setLayoutParams(textLayoutParams);
        tv_type_document.setLayoutParams(textLayoutParams);
        tv_type_software.setLayoutParams(textLayoutParams);

        textViewList.add(tv_type_standard);
        textViewList.add(tv_type_book);
        textViewList.add(tv_type_document);
        textViewList.add(tv_type_software);

        addView(myView);

        tv_type_standard.setOnClickListener(this);
        tv_type_book.setOnClickListener(this);
        tv_type_document.setOnClickListener(this);
        tv_type_software.setOnClickListener(this);
    }


    @Override
    public void onClick(View view) {
        int viewId = view.getId();
        switch (viewId) {
            case R.id.tv_type_standard:
                switchTextButton(tv_type_standard);
                if (groupTypeInterface != null)
                    groupTypeInterface.clickType(1);
                break;
            case R.id.tv_type_book:
                switchTextButton(tv_type_book);
                if (groupTypeInterface != null)
                    groupTypeInterface.clickType(2);
                break;
            case R.id.tv_type_document:
                switchTextButton(tv_type_document);
                if (groupTypeInterface != null)
                    groupTypeInterface.clickType(3);
                break;
            case R.id.tv_type_software:
                switchTextButton(tv_type_software);
                if (groupTypeInterface != null)
                    groupTypeInterface.clickType(4);
                break;
        }

    }


    /**
     * 1 标准,2 图书,3 使用文档,4 软件工具
     *
     * @param type
     */
    public void switchTypeButton(int type) {
        if (type == 1) {
            switchTextButton(tv_type_standard);
        } else if (type == 2) {
            switchTextButton(tv_type_book);
        } else if (type == 3) {
            switchTextButton(tv_type_document);
        } else if (type == 4) {
            switchTextButton(tv_type_software);
        }
    }


    private void switchTextButton(TextView tmp) {
        for (TextView textView : textViewList) {
            if (textView.getId() == tmp.getId()) {
                textView.setSelected(true);
            } else {
                textView.setSelected(false);
            }
        }
    }


    private GroupTypeInterface groupTypeInterface;

    public void setGroupTypeInterface(GroupTypeInterface groupTypeInterface) {
        this.groupTypeInterface = groupTypeInterface;
    }

    public interface GroupTypeInterface {
        void clickType(int type);
    }


}
